/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 * Version：V1.0.2(20230701).
 */
package top.hiai.index;

import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import top.hiai.common.account.UserRoleService;
import top.hiai.common.model.Article;
import top.hiai.common.model.Comments;
import top.hiai.common.model.Reply;
import top.hiai.common.model.User;
import top.hiai.common.util.JsoupFilter;
import top.hiai.my.newsfeed.ReferMeKit;

import java.util.Date;
import java.util.List;

/**
 * @Author www.hiai.top
 * @Email goodsking@163.com
 * @Message 更多资源尽在www.hiai.top
 * @Introduction 类的介绍
 */

public class IndexService {

    public static final IndexService me = new IndexService();
    private final String indexCacheName = "index";

    private User userDao = new User().dao();
    private Article articleDao = new Article().dao();
    private Comments commentsDao = new Comments().dao();
    private Reply replyDao = new Reply().dao();

    /**
     * 获取文章列表
     *
     * @param pageNumber
     * @return
     */
    public Page<Article> paginate(int pageNumber, int pageSize, int isPay) {
        Page<Article> articlePage = articleDao.paginate(pageNumber, pageSize,
                "select a.aid,a.title,a.is_hot,a.addtime,a.content,a.views,a.favoriteCount,a.replyCount,a.is_pay,a.points,a.download,a.password,u.uid,u.nickName,u.avatar,u.is_auth,u.is_vip",
                "from bbs_article as a , bbs_user as u where a.uid=u.uid and a.status=? and a.is_pay=? and is_del=0 order by a.is_hot desc,a.addtime desc", Article.STATUS_OK, isPay);
        // 列表页显示 content 的摘要信息需要过滤为纯文本，去除所有标记，限制标题输出的长度
        JsoupFilter.filterArticleList(articlePage.getList(), 50, 120);
        return articlePage;
    }

    /**
     * 获取热门文章，根据浏览量排序，默认获取6个
     *
     * @return
     */
    public List<Article> getArticleHotList() {
        List<Article> articleList = articleDao.find(" select aid,title,content "
                + "from bbs_article "
                + "where status=1 and is_del=0 order by views desc,addtime desc limit 8");
        JsoupFilter.filterArticleList(articleList, 25, 120);
        return articleList;
    }

    /**
     * 获取首页侧边栏的热门话题，获取文章的数量，暂时根据sort排序,获取6个
     *
     * @return
     */
    public List<Comments> getCommentsHotList() {
        //select c.name, count(a.aid) from bbs_comments as c left join bbs_article as a on c.tid=a.type_id group by c.tid;
        //select c.name ,max(c.tid) as maxId,count(a.aid) as msgCount from bbs_comments as c,bbs_article as a where c.tid=a.type_id group by c.tid;
        return commentsDao.find(" select c.tid,c.name,c.images,count(a.aid) as msgCount  "
                + " from bbs_comments as c left join bbs_article as a on c.tid=a.type_id and a.status=1 and a.is_del=0 and c.is_del=0 and c.pid=0 group by c.tid order by c.sort desc, msgCount desc limit 6");
    }

    /**
     * 获取首页侧边栏的热门用户，根据威望排序，获取8个人
     *
     * @return
     */
    public List<User> getUserHotList() {
        return userDao.find(" select uid,nickName,avatar "
                + "from bbs_user "
                + "where status=1 order by prestige desc limit 15");
    }

    /**
     * 根绝前台传过来的ID获取文章,用户文章的详情页面使用
     *
     * @param aid
     * @return
     */
    public Article getDetailArticle(int aid) {
        return articleDao.findFirst(" select a.aid,a.title,a.addtime,a.views,a.content,a.favoriteCount,a.replyCount,a.type_id as typeId,a.is_pay,a.points,a.download,a.password,a.is_del,u.uid,u.nickName,u.avatar,u.sign,u.is_auth,c.name"
                + " from bbs_article as a , bbs_user as u , bbs_comments as c "
                + " where a.aid=? and a.uid=u.uid and a.type_id = c.tid ", aid);
    }

    /**
     * 用户浏览文章，文章浏览量自动加1
     * 后期要使用别的方法，防止用户恶意刷新
     *
     * @param aid
     */
    public void initArticleView(int aid) {
        Db.update(" update bbs_article set views = views+1 where aid = ? ", aid);
    }

    /**
     * 获取文章的评论信息
     *
     * @param pageNumber
     * @return
     */
    public Page<Reply> replyPaginate(int pageNumber, int pageSize, int aid) {
        Page<Reply> replyPage = replyDao.paginate(pageNumber, pageSize, "select r.rid,r.aid,r.content,r.addtime,u.uid,u.nickName,u.avatar",
                "from bbs_reply as r,bbs_user as u where r.uid=u.uid and r.status=1 and r.aid = ? order by r.addtime desc", aid);
        return replyPage;
    }

    /**
     * 删除文章的时候清除数据
     */
    public void clearCache() {
        CacheKit.removeAll(indexCacheName);
    }

    /**
     * 保存用户的回复内容
     *
     * @param aid
     * @param accountId
     * @param content
     * @return
     */
    @SuppressWarnings("unused")
    public Ret saveArticleReply(Integer aid, Integer accountId, String content) {
        Reply reply = new Reply();
        reply.setAid(aid);
        reply.setUid(accountId);
        reply.setContent(content);
        reply.setAddtime(new Date());
        List<Integer> referAccounts = ReferMeKit.buildAtMeLink(reply);
        reply.save();

        // 添加分享回复动态消息
//        NewsFeedService.me.createShareReplyNewsFeed(accountId, reply, referAccounts);

        //添加文章中的评论条数
        int temp = Db.update(" update bbs_article set replyCount=replyCount+1 where aid=? limit 1", aid);
        if (temp < 0) {
            return Ret.fail().set("msg", "评论失败。");
        }
        //评论成功后给用户添加积分
        UserRoleService.me.setAddUserPoints(UserRoleService.ADD_REPLY, accountId);
        return Ret.ok("reply", reply);
    }
}